/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.factory.service.impl;

import java.util.List;
import org.apache.log4j.Logger;
import org.factory.domain.FactoryItems;
import org.factory.service.FactoryService;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.shop.utils.HibernateUtils;

/**
 *
 * @author Vinnu
 */
public class FactoryServiceImpl implements FactoryService {

    private static Session session = null;
    private static FactoryServiceImpl impl = new FactoryServiceImpl();
    //private static final Logger log = Logger.getLogger(FactoryServiceImpl.class);

    private FactoryServiceImpl() {
    }

    private Session getSession() {
        session = HibernateUtils.getSession();
        return session;
    }

    private void closeSession(Session s, Transaction t) {
        HibernateUtils.closeSession(s, t);
    }

    @Override
    public void addItemsToFactory(FactoryItems items) {
        session = getSession();
        Transaction transaction = session.beginTransaction();
        session.save(items);
        closeSession(session, transaction);
    }

    @Override
    public List<FactoryItems> getAllFactoryItems() {
        session = getSession();
        Transaction transaction = session.beginTransaction();
        Query query = session.createQuery("from Factoryitems");
        List<FactoryItems> list = query.list();
        closeSession(session, transaction);
        return list;
    }

    public static FactoryService getService() {
        return impl;
    }
}
